Skip to content

Read structs with defaults that hold a Validator without a valid default - #780

Draft
clockblocker wants to merge 1 commit into
munich-data-engineering:mainfrom
clockblocker:fix-700-deferred-defaults
Draft

clockblocker wants to merge 1 commit into
munich-data-engineering:mainfrom
clockblocker:fix-700-deferred-defaults

Conversation

@clockblocker

@clockblocker clockblocker commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #700: allows reading and generating JSON Schema for structs that mix rfl::DefaultVal / rfl::DefaultIfMissing with rfl::Validator fields whose default value is invalid, while keeping rfl::Validator trivially copyable.

Example

using Foods = rfl::Validator<std::vector<std::string>, rfl::Size<rfl::Minimum<1>>>;

struct Person {
  rfl::DefaultVal<std::string> name = "Homer Simpson";
  Foods favorite_foods;  // Foods{} throws (size 0 < 1)
};

// Before (main): fails before looking at input ("Could not default-construct Person...")
// After:
rfl::json::read<Person>(R"({"favorite_foods": ["donuts"]})");
// -> ok: name="Homer Simpson", favorite_foods={"donuts"}

rfl::json::read<Person>(R"({})");
// -> error: Field named 'favorite_foods' not found.

rfl::json::read<Person, rfl::DefaultIfMissing>(R"({})");
// -> error: Field named 'favorite_foods' not found, and its default value is invalid: ...

rfl::json::to_schema<Person>();
// -> "name" keeps "default": "Homer Simpson"; "favorite_foods" is in "required"
// Person{} outside read() still throws.

How it works

  • When T{} throws during read_struct_with_default, the parser retries T{} inside a thread-local deferral scope (rfl::internal::DeferredDefaults, exported via RFL_API in src/rfl/internal/DeferredDefaults.cpp) where Validator() keeps its invalid default and increments a counter instead of throwing.
  • After construction, the parser walks T to confirm it holds at least as many invalid Validators as were deferred; after reading, it walks every missing field's default and fails if any invalid Validator or uncheckable opaque type remains.
  • Validator keeps defaulted copy/move constructors (stays trivially copyable; zero copy-path overhead).

Known limits

  • A default member initializer that reads a deferred Validator (e.g. int b = a.value() * 2;) observes its invalid value.
  • A discarded temporary Validator (e.g. int port = Positive().value();) fails the read unless another invalid Validator copy offsets the count.
  • A Validator inside an opaque class the walker cannot inspect fails the read (as on main), and a missing field of such an opaque type also fails when deferral is active.
  • A try/catch fallback inside a member initializer receives no exception while deferral is active, so that field must be supplied in the input.

AI-authored.

@clockblocker

Copy link
Copy Markdown
Contributor Author

Converting this to a draft so that #786 can be reviewed first. Both PRs change to_schema for structs with defaults.

Once #786 lands, I'll rebase this PR onto it and drop the commit "Generate a schema for structs that cannot be default-constructed", which #786 makes redundant. #786 doesn't fix #700, so the rest of this PR stays as it is.

@clockblocker
clockblocker force-pushed the fix-700-deferred-defaults branch from 9fa1d1d to 2d33403 Compare November 21, 2026 09:45

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Reading JSON into a struct with a DefaultVal field and a size validator field always raises a size validation error

1 participant